package com.linkcard.list.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkcard.commen.tools.Helper;
import com.linkcard.commen.tools.Page;
import com.linkcard.list.manager.BuycardManager;
import com.linkcard.list.model.Buycard;
import com.opensymphony.xwork2.ActionSupport;

public class BuycardAction extends ActionSupport {

	private BuycardManager buycardManager=null;
	private Buycard buycard=null;
	private Page pageObj=null;
	private int page;
	private int rows;
	@Override
	public String execute() throws Exception {
		return "success";
	}
	//添加办卡信息
	public String addBuycardInfo() throws Exception{
		buycard.setReturnVisit(0);
		buycard.setName(Helper.decode(buycard.getName()));
		buycard.setRemark(Helper.decode(buycard.getRemark()));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		buycard.setDatetime(sdf.format(new Date()));
		boolean isSuccess=this.buycardManager.addBuycardInfo(buycard);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//获得办卡信息列表
	public String buycardListForJson() throws Exception{
		pageObj=new Page();
		pageObj.setCurrentPage(page);
		pageObj.setPageSize(rows);
		List list=this.buycardManager.buycardList(pageObj);
		JSONObject json=new JSONObject();
		json.put("total", pageObj.getRowCount());
		JSONArray jsonArray=new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			Buycard buycard=(Buycard)list.get(i);
			JSONObject tempObject=new JSONObject();
			tempObject.put("id", buycard.getId());
			tempObject.put("name", buycard.getName());
			tempObject.put("sex", (buycard.getSex()==0)?"男":"女");
			tempObject.put("amount", buycard.getAmount());
			tempObject.put("cardType", buycard.getCardType());
			tempObject.put("tel", buycard.getTel());
			tempObject.put("remark", buycard.getRemark());
			tempObject.put("datetime", buycard.getDatetime());
			tempObject.put("idcard", buycard.getIdcard());
			tempObject.put("returnVisit",( buycard.getReturnVisit()==0)?"<a href='javascript:returnVisit("+buycard.getId()+",1)'>未回访</a>":"<a href='javascript:returnVisit("+buycard.getId()+",0)'>回访</a>");
			tempObject.put("opt", "<a href='javascript:void(0);' onclick='deleteBuycard("+buycard.getId()+")'>删除</a>");
			jsonArray.add(tempObject);
		}
		json.put("rows", jsonArray);
		backText(json.toString());
		return"Data";
	}
	//回访
	public String returnVisit() throws Exception
	{
		boolean isSuccess=this.buycardManager.returnVisit(buycard);
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//删除
	public String delteBuycard() throws Exception
	{
		boolean isSuccess=this.buycardManager.delteBuycard(buycard.getId());
		JSONObject json=new JSONObject();
		json.put("info", isSuccess);
		backText(json.toString());
		return "Data";
	}
	//返回文本
	public void backText(String data) throws Exception
	{
		ServletActionContext.getResponse().setContentType ("text/html;charset=utf-8");
		
		  PrintWriter out = ServletActionContext.getResponse().getWriter();
		  out.print(data);
		  out.flush();
		  out.close();
	}
	public BuycardManager getBuycardManager() {
		return buycardManager;
	}
	public void setBuycardManager(BuycardManager buycardManager) {
		this.buycardManager = buycardManager;
	}
	public Buycard getBuycard() {
		return buycard;
	}
	public void setBuycard(Buycard buycard) {
		this.buycard = buycard;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setPageObj(Page pageObj) {
		this.pageObj = pageObj;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}

}
